<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <a-button v-permission="permission.add" class="filter-item" type="primary" icon="plus" @click="crudCreate" >新增</a-button>
    </div>
    <!--表格渲染-->
    <a-table
      size="middle"
      row-key="id"
      :columns="columns"
      :data-source="tableData"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange"
      @expand="expand"
    >
      <span slot="action" slot-scope="text, record" >
        <udOperation
          :data="record"
          :permission="permission"
          :disabled-dle="record.id === 1"
          @crudUpdate="crudUpdate"
          @crudDelete="crudDelete(record.id)"
        />
      </span>
    </a-table>
    <!--表单组件-->
    <dept-update-modal ref="updateForm" @closeModal="closeModal"></dept-update-modal>
  </div>
</template>

<script>
import ApiTree from '@/api/system/dept';
import udOperation from '@crud/UD.operation';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

import { Slime, crud } from '@/mixins/slime';
import DeptUpdateModal from './deptUpdateModal';

export default {
  name: 'Dept',
  components: { udOperation, DeptUpdateModal, },
  mixins: [Slime],
  // 设置数据字典
  // dicts: ['dept_status'],
  data() {
    return {
      // table
      tableData: [],
      columns: [
        {
          title: '名称',
          align: 'left',
          dataIndex: 'name',
        },
        {
          title: '排序',
          align: 'left',
          dataIndex: 'sort',
        },
        {
          title: '创建日期',
          align: 'left',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 权限
      permission: {
        add: ['admin', 'dept:add'],
        edit: ['admin', 'dept:edit'],
        del: ['admin', 'dept:del'],
      },
    };
  },
  created() {
    crud(ApiTree)
    this.loadData();
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      // ApiTree.getDeptTree(0).then((res) => {
      //   let data = res.result;
      //   const tableData = data.map(function (item) {
      //     if (item.subCount > 0) {
      //       let children = [];
      //       item = { ...item, children: children };
      //     }
      //     return item;
      //   });
      //   this.tableData = tableData;
      // });
    },
    // 点击展开
    expand(expanded, record) {
      // ApiTree.getDeptTree(record.id).then((res) => {
      //   let data = res.result;
      //   const tableData = data.map(function (item) {
      //     if (item.subCount > 0) {
      //       let children = [];
      //       item = { ...item, children: children };
      //     }
      //     return item;
      //   });
      //   this.childSearch(this.tableData, record.id, tableData);
      // });
    },
    // 对于点击展开的对象进行赋值
    childSearch(obj, id, childrenVal) {
      let existence = false;
      obj.map((item) => {
        if (item.id == id) {
          existence = true;
          if (item.subCount > 0) {
            item.children = childrenVal;
          }
        }
        if (!existence && item.subCount > 0) {
          this.childSearch(item.children, id, childrenVal);
        }
      });
    },
    checkboxT(row, rowIndex) {
      return row.id !== 1;
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
